%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                         %
%                   Entrant Employment Distribution                       %
%                                                                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function EntrantDist = Entrant_Dist(p, F1, Deltagrid, LPNwMLam)

% --------------------------- Description --------------------------------- 

    % This function calculates the distribution of firm size (in terms of
    % employees) for firms with age 0 and 1 product (entrants). For an
    % overview of this function, please see Section (RP-2.5.2).

%--------------------------------------------------------------------------
%              1. Conditional Distribution with Competitors               %
%--------------------------------------------------------------------------

    % Define the number of points in the gap distribution
    Deltagrid_pts = length(Deltagrid);

    % We denote the distribution with competitors by Fc
    Fc = F1;

    % We take differences to obtain the associated density
    Fc_dens = zeros(Deltagrid_pts, p.n3);
    Fc_dens(:, 1:end-1) = diff(Fc, 1, 2);
    Fc_dens = [diff(Fc_dens, 1, 1); zeros(1, p.n3)];

    % Compute the marginal distribution - denominator in Equation (RP-39)
    Fdelta_marginal = sum(Fc_dens, 2);

    % Compute the conditional distribution accordingly - see Equation (RP-39)
    Fc_cond = cumsum(Fc_dens, 2) ./ repmat(Fdelta_marginal, 1, p.n3);

    % For the last possible gap, there are NaNs - repeat previous line
    Fc_cond(end, :) = Fc_cond(end-1, :);  

%--------------------------------------------------------------------------
%            2. Conditional Distribution without Competitors              %
%--------------------------------------------------------------------------

    % Transformed productivity equivalent to minimum relative efficiency
    MinEff = log(p.wmin);

    % Truncate the grid for efficiency to set cdf to zero before MinEff
    grid_aux = max(MinEff, p.qgrid);

    % Initial condition for the differential equation - see Equation (RP-36)
    FNC_cond = 1 - (p.wmin*exp(-grid_aux)).^p.varrho;

%--------------------------------------------------------------------------
%                     3. Defining the appropriate grids                   %
%--------------------------------------------------------------------------

    % Varieties without competitors: grid for log(y) implied in our q grid
    ygrid_endog_NC = (p.s-1)*p.qgrid_adj - p.s*log(p.s/(p.s-1)) + log(LPNwMLam);

    % Varieties with competitors: grid for log(y) implied in our q grid
    ygrid_endog_C = (p.s-1)*p.qgrid_adj - p.s*log(p.l) + log(LPNwMLam);

    % The target grid for employment comes from Params
    yvec = p.yvec;

    % Create vectors to store the interpolated distributions
    FNC_cond_grid = zeros(1, p.ypoints);
    FC_cond_grid = zeros(1, p.ypoints);

    % Interpolate the distribution without competitors to the grid
    FNC_cond_grid = (1-p.alpha)*interp1(ygrid_endog_NC, FNC_cond, yvec, 'linear');

    % Interpolate the distribution with competitors to the grid
    FC_cond_grid = p.alpha*interp1(ygrid_endog_C, Fc_cond(1,:), yvec, 'linear');

    % Since ygrid starts before the endogenous grids, there are NaNs
    FNC_cond_grid(isnan(FNC_cond_grid)) = 0;
    FC_cond_grid(isnan(FC_cond_grid)) = 0;

%--------------------------------------------------------------------------
%                     4. Computing Entrant Distribution                   %
%--------------------------------------------------------------------------

    % Compute the employment distribution
    EntrantDist = FNC_cond_grid + FC_cond_grid;

end